<?php

require_once 'calendar.php';
require_once 'opendb.php';

class CalendarManager
{
    public static function addCalendar($calendar)
    {
        //$calendar = mysql_real_escape_string($calendar);
      
        $userID = $calendar->getUserID();
        $eventName = $calendar->getEventName();
        $date = $calendar->getDate();
        $time = $calendar->getTime();

        $descript = $calendar->getDescript();
        $Location = $calendar->getLocation();
        $AlertBool = $calendar->getAlertBool();

        $insert = mysql_query("INSERT INTO calendar_events VALUES('$userID', NULL, '$eventName', '$date', '$time', '$descript', '$Location', '$Alert_Bool')") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
        {
            echo "<br>The calendar event has been added successfully.";
        } else
            echo "<br>The calendar event has not been added successfully.";
        echo "<br>";
    }

    public static function editCalendar($calendar)
    {
        //$calendar = mysql_real_escape_string($calendar);
        $eventName = $calendar->geteventName();
        $userID = $calendar->getUserID();
        $eventName = $calendar->getEventName();
        $date = $calendar->getDate();
        $time = $calendar->getTime();
        $descript = $calendar->getDescript();
        $calendarID = $calendar->getCalendarID();
        $AlertBool = $calendar->getAlertBool();
        $Location = $calendar->getLocation();
        $update = mysql_query("UPDATE calendar_events SET UserID = '$userID', eventName = '$eventName', date = '$date', time = '$time', descript = '$descript', AlertBool = '$AlertBool', Location = '$Location' WHERE calendarID = '$calendarID' LIMIT 1") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The calendar has been updated successfully.";
        else
            echo "The calendar has not been updated successfully.";
        echo "<br>";
    }

    public static function deleteCalendar($calendar)
    {
       // $calendar = mysql_real_escape_string($calendar);
        $calendarID = $calendar->getCalendarID();
        $delete = mysql_query("DELETE FROM calendar_events WHERE calendarID = '$calendarID' LIMIT 1");
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The calendar event has been successfully deleted. <br>";
        else
            echo "The calendar event has not been successfully deleted. <br>";
        echo "<br>";
    }

    public static function getCalendar($userID)
    {
        //$userID = mysql_real_escape_string(userID);
        $select = mysql_query("SELECT * FROM calendar_events WHERE UserID = '$userID'") or die(mysql_error());
        $calendarArray = array();
        while ($row = mysql_fetch_array($select))
        {
            $calendar = new Calendar();
            $calendar = calendarManager::getCalendarEvent($row['calendarID']);
            $calendarArray[] = $calendar;
        }
        return $calendarArray;
    }

    public static function getCalendarEvent($calendarID)
    {
        //$calendarID = mysql_real_escape_string($calendarID);
        $select = mysql_query("SELECT * FROM calendar_events WHERE calendarID = '$calendarID'") or die(mysql_error());
        $result = mysql_num_rows($select);
        if ($result > 0)
        {
            $calendarInfo = mysql_fetch_array($select);
            $calendar = new Calendar();
            $calendar->setUserID($calendarInfo['UserID']);
            $calendar->setCalendarID($calendarInfo['calendarID']);
            $calendar->setEventName($calendarInfo['eventName']);
            $calendar->setDate($calendarInfo['date']);
            $calendar->setTime($calendarInfo['time']);
            $calendar->setDescript($calendarInfo['descript']);
            $calendar->setLocation($calendarInfo['Location']);
            $calendar->setAlertBool($calendarInfo['AlertBool']);

            return $calendar;
        } else
            echo "There is no calendar event with that ID.";
    }
}
?>